package com.jbpm.wf.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.emms.common.util.BaseUtil;
import com.jbpm.action.BaseAction;
import com.jbpm.util.EntityUtils;
import com.jbpm.util.JsonBuilder;
import com.jbpm.wf.model.ProcessFunMount;
import com.jbpm.wf.model.TaskEventInfo;
import com.jbpm.wf.model.TaskFuncMount;
@Component("wfTaskEventAction")
@Scope("prototype")
public class WfTaskEventAction extends BaseAction {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 3945631620828445493L;
	private TaskEventInfo taskEventInfo=new TaskEventInfo();

	public void load(){
		String parentType=request.getParameter("parentType");
		if(!BaseUtil.isEmpty(ids) && !BaseUtil.isEmpty(parentType)){
			List<TaskEventInfo> tasks=new ArrayList<TaskEventInfo>();			
			if(parentType.equalsIgnoreCase("ProcessFunMount")){
				ProcessFunMount process=(ProcessFunMount) pcManagerTemplate.findById(ProcessFunMount.class, ids);				
				if(EntityUtils.isNotNull(process)){
					tasks=process.getTaskEventInfos();
					if(limit>=tasks.size()){
						limit=tasks.size();
					}
					tasks=tasks.subList(start, limit);
				}
			}else if(parentType.equalsIgnoreCase("TaskFuncMount")){
				TaskFuncMount taskFuncMount=(TaskFuncMount) pcManagerTemplate.findById(TaskFuncMount.class, ids);
				if(EntityUtils.isNotNull(taskFuncMount)){
					tasks=taskFuncMount.getTaskEventInfos();
					if(limit>=tasks.size()){
						limit=tasks.size();
					}
					tasks=tasks.subList(start, limit);
				}
			}
			Integer count=tasks.size();
			strData=JsonBuilder.getInstance().buildObjListToJson((long)count,tasks , true);
			toWrite(strData);
		}else{
			strData=JsonBuilder.getInstance().buildObjListToJson((long)0,new ArrayList<TaskEventInfo>() , true);
			toWrite(strData);
		}
	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		String parentType=request.getParameter("parentType");
		String foreignKey=request.getParameter("foreignKey");
		if(!!BaseUtil.isEmpty(parentType) || !!BaseUtil.isEmpty(foreignKey)){
			return taskEventInfo;
		}
		if(parentType.equalsIgnoreCase("ProcessFunMount")){
			ProcessFunMount process=new ProcessFunMount();
			process.setId(foreignKey);
			taskEventInfo.setProcessFunMount(process);
		}else if(parentType.equalsIgnoreCase("TaskFuncMount")){
			TaskFuncMount taskFuncMount=new TaskFuncMount();
			taskFuncMount.setId(foreignKey);
			taskEventInfo.setTaskFuncMount(taskFuncMount);
		}
		return taskEventInfo;
	}

	
}
